<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Trustworthy #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# OPTIONS_GHC -funbox-strict-fields #-}</span><span>
</span><span id="line-4"></span><span>
</span><span id="line-5"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- Module      :  Control.Concurrent.QSemN</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2001</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- </span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Portability :  non-portable (concurrency)</span><span>
</span><span id="line-14"></span><span class="hs-comment">--</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Quantity semaphores in which each thread may wait for an arbitrary</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- \&quot;amount\&quot;.</span><span>
</span><span id="line-17"></span><span class="hs-comment">--</span><span>
</span><span id="line-18"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-19"></span><span>
</span><span id="line-20"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Control.Concurrent.QSemN</span><span>
</span><span id="line-21"></span><span>        </span><span class="hs-special">(</span><span>  </span><span class="annot"><span class="hs-comment">-- * General Quantity Semaphores</span></span><span>
</span><span id="line-22"></span><span>          </span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier">QSemN</span></a></span><span class="hs-special">,</span><span>        </span><span class="hs-comment">-- abstract</span><span>
</span><span id="line-23"></span><span>          </span><span class="annot"><a href="Control.Concurrent.QSemN.html#newQSemN"><span class="hs-identifier">newQSemN</span></a></span><span class="hs-special">,</span><span>     </span><span class="hs-comment">-- :: Int   -&gt; IO QSemN</span><span>
</span><span id="line-24"></span><span>          </span><span class="annot"><a href="Control.Concurrent.QSemN.html#waitQSemN"><span class="hs-identifier">waitQSemN</span></a></span><span class="hs-special">,</span><span>    </span><span class="hs-comment">-- :: QSemN -&gt; Int -&gt; IO ()</span><span>
</span><span id="line-25"></span><span>          </span><span class="annot"><a href="Control.Concurrent.QSemN.html#signalQSemN"><span class="hs-identifier">signalQSemN</span></a></span><span>   </span><span class="hs-comment">-- :: QSemN -&gt; Int -&gt; IO ()</span><span>
</span><span id="line-26"></span><span>      </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-27"></span><span>
</span><span id="line-28"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Concurrent.MVar.html"><span class="hs-identifier">Control.Concurrent.MVar</span></a></span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier">MVar</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier">newEmptyMVar</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier">takeMVar</span></a></span><span>
</span><span id="line-29"></span><span>                          </span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier">tryPutMVar</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#isEmptyMVar"><span class="hs-identifier">isEmptyMVar</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-30"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Exception.html"><span class="hs-identifier">Control.Exception</span></a></span><span>
</span><span id="line-31"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Monad.html"><span class="hs-identifier">Control.Monad</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Base.html#when"><span class="hs-identifier">when</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-32"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.IORef.html"><span class="hs-identifier">Data.IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier">IORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.IORef.html#newIORef"><span class="hs-identifier">newIORef</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier">atomicModifyIORef</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-33"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="System.IO.Unsafe.html"><span class="hs-identifier">System.IO.Unsafe</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.IO.Unsafe.html#unsafePerformIO"><span class="hs-identifier">unsafePerformIO</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-34"></span><span>
</span><span id="line-35"></span><span class="hs-comment">-- | 'QSemN' is a quantity semaphore in which the resource is acquired</span><span>
</span><span id="line-36"></span><span class="hs-comment">-- and released in units of one. It provides guaranteed FIFO ordering</span><span>
</span><span id="line-37"></span><span class="hs-comment">-- for satisfying blocked `waitQSemN` calls.</span><span>
</span><span id="line-38"></span><span class="hs-comment">--</span><span>
</span><span id="line-39"></span><span class="hs-comment">-- The pattern</span><span>
</span><span id="line-40"></span><span class="hs-comment">--</span><span>
</span><span id="line-41"></span><span class="hs-comment">-- &gt;   bracket_ (waitQSemN n) (signalQSemN n) (...)</span><span>
</span><span id="line-42"></span><span class="hs-comment">--</span><span>
</span><span id="line-43"></span><span class="hs-comment">-- is safe; it never loses any of the resource.</span><span>
</span><span id="line-44"></span><span class="hs-comment">--</span><span>
</span><span id="line-45"></span><span class="hs-keyword">data</span><span> </span><span id="QSemN"><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-var">QSemN</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="QSemN"><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-var">QSemN</span></a></span></span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="GHC.IORef.html#IORef"><span class="hs-identifier hs-type">IORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">(</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span class="hs-special">]</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-46"></span><span>
</span><span id="line-47"></span><span class="hs-comment">-- The semaphore state (i, xs, ys):</span><span>
</span><span id="line-48"></span><span class="hs-comment">--</span><span>
</span><span id="line-49"></span><span class="hs-comment">--   i is the current resource value</span><span>
</span><span id="line-50"></span><span class="hs-comment">--</span><span>
</span><span id="line-51"></span><span class="hs-comment">--   (xs,ys) is the queue of blocked threads, where the queue is</span><span>
</span><span id="line-52"></span><span class="hs-comment">--           given by xs ++ reverse ys.  We can enqueue new blocked threads</span><span>
</span><span id="line-53"></span><span class="hs-comment">--           by consing onto ys, and dequeue by removing from the head of xs.</span><span>
</span><span id="line-54"></span><span class="hs-comment">--</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- A blocked thread is represented by an empty (MVar ()).  To unblock</span><span>
</span><span id="line-56"></span><span class="hs-comment">-- the thread, we put () into the MVar.</span><span>
</span><span id="line-57"></span><span class="hs-comment">--</span><span>
</span><span id="line-58"></span><span class="hs-comment">-- A thread can dequeue itself by also putting () into the MVar, which</span><span>
</span><span id="line-59"></span><span class="hs-comment">-- it must do if it receives an exception while blocked in waitQSemN.</span><span>
</span><span id="line-60"></span><span class="hs-comment">-- This means that when unblocking a thread in signalQSemN we must</span><span>
</span><span id="line-61"></span><span class="hs-comment">-- first check whether the MVar is already full.</span><span>
</span><span id="line-62"></span><span>
</span><span id="line-63"></span><span class="hs-comment">-- |Build a new 'QSemN' with a supplied initial quantity.</span><span>
</span><span id="line-64"></span><span class="hs-comment">--  The initial quantity must be at least 0.</span><span>
</span><span id="line-65"></span><span class="annot"><a href="Control.Concurrent.QSemN.html#newQSemN"><span class="hs-identifier hs-type">newQSemN</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-type">QSemN</span></a></span><span>
</span><span id="line-66"></span><span id="newQSemN"><span class="annot"><span class="annottext">newQSemN :: Int -&gt; IO QSemN
</span><a href="Control.Concurrent.QSemN.html#newQSemN"><span class="hs-identifier hs-var hs-var">newQSemN</span></a></span></span><span> </span><span id="local-6989586621679554254"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554254"><span class="hs-identifier hs-var">initial</span></a></span></span><span>
</span><span id="line-67"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554254"><span class="hs-identifier hs-var">initial</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String -&gt; IO QSemN
forall (m :: * -&gt; *) a. MonadFail m =&gt; String -&gt; m a
</span><a href="Control.Monad.Fail.html#fail"><span class="hs-identifier hs-var">fail</span></a></span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;newQSemN: Initial quantity must be non-negative&quot;</span></span><span>
</span><span id="line-68"></span><span>  </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-69"></span><span>      </span><span id="local-6989586621679554252"><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554252"><span class="hs-identifier hs-var">sem</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(Int, [(Int, MVar ())], [(Int, MVar ())])
-&gt; IO (IORef (Int, [(Int, MVar ())], [(Int, MVar ())]))
forall a. a -&gt; IO (IORef a)
</span><a href="GHC.IORef.html#newIORef"><span class="hs-identifier hs-var">newIORef</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554254"><span class="hs-identifier hs-var">initial</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">)</span><span>
</span><span id="line-70"></span><span>      </span><span class="annot"><span class="annottext">QSemN -&gt; IO QSemN
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())]) -&gt; QSemN
</span><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-var">QSemN</span></a></span><span> </span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554252"><span class="hs-identifier hs-var">sem</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span><span class="hs-comment">-- An unboxed version of Maybe (MVar a)</span><span>
</span><span id="line-73"></span><span class="hs-keyword">data</span><span> </span><span id="MaybeMV"><span class="annot"><a href="Control.Concurrent.QSemN.html#MaybeMV"><span class="hs-identifier hs-var">MaybeMV</span></a></span></span><span> </span><span id="local-6989586621679554304"><span class="annot"><a href="#local-6989586621679554304"><span class="hs-identifier hs-type">a</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="JustMV"><span class="annot"><a href="Control.Concurrent.QSemN.html#JustMV"><span class="hs-identifier hs-var">JustMV</span></a></span></span><span> </span><span class="hs-glyph">!</span><span class="hs-special">(</span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679554304"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">|</span><span> </span><span id="NothingMV"><span class="annot"><a href="Control.Concurrent.QSemN.html#NothingMV"><span class="hs-identifier hs-var">NothingMV</span></a></span></span><span>
</span><span id="line-74"></span><span>
</span><span id="line-75"></span><span class="hs-comment">-- |Wait for the specified quantity to become available</span><span>
</span><span id="line-76"></span><span class="annot"><a href="Control.Concurrent.QSemN.html#waitQSemN"><span class="hs-identifier hs-type">waitQSemN</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-type">QSemN</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- We need to mask here. Once we've enqueued our MVar, we need</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- to be sure to wait for it. Otherwise, we could lose our</span><span>
</span><span id="line-79"></span><span class="hs-comment">-- allocated resource.</span><span>
</span><span id="line-80"></span><span id="waitQSemN"><span class="annot"><span class="annottext">waitQSemN :: QSemN -&gt; Int -&gt; IO ()
</span><a href="Control.Concurrent.QSemN.html#waitQSemN"><span class="hs-identifier hs-var hs-var">waitQSemN</span></a></span></span><span> </span><span id="local-6989586621679554249"><span class="annot"><span class="annottext">qs :: QSemN
</span><a href="#local-6989586621679554249"><span class="hs-identifier hs-var">qs</span></a></span></span><span class="hs-glyph">@</span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-type">QSemN</span></a></span><span> </span><span id="local-6989586621679554248"><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554248"><span class="hs-identifier hs-var">m</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679554247"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554247"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">IO () -&gt; IO ()
forall a. IO a -&gt; IO a
</span><a href="GHC.IO.html#mask_"><span class="hs-identifier hs-var">mask_</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; IO () -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-81"></span><span>    </span><span class="hs-comment">-- unsafePerformIO and not unsafeDupablePerformIO. We must</span><span>
</span><span id="line-82"></span><span>    </span><span class="hs-comment">-- be sure to wait on the same MVar that gets enqueued.</span><span>
</span><span id="line-83"></span><span>  </span><span id="local-6989586621679554245"><span class="annot"><span class="annottext">MaybeMV ()
</span><a href="#local-6989586621679554245"><span class="hs-identifier hs-var">mmvar</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())])
    -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ()))
-&gt; IO (MaybeMV ())
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier hs-var">atomicModifyIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554248"><span class="hs-identifier hs-var">m</span></a></span><span> </span><span class="annot"><span class="annottext">(((Int, [(Int, MVar ())], [(Int, MVar ())])
  -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ()))
 -&gt; IO (MaybeMV ()))
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())])
    -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ()))
-&gt; IO (MaybeMV ())
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span> </span><span class="hs-special">(</span><span id="local-6989586621679554244"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554244"><span class="hs-identifier hs-var">i</span></a></span></span><span class="hs-special">,</span><span id="local-6989586621679554243"><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554243"><span class="hs-identifier hs-var">b1</span></a></span></span><span class="hs-special">,</span><span id="local-6989586621679554242"><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554242"><span class="hs-identifier hs-var">b2</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
forall a. IO a -&gt; a
</span><a href="GHC.IO.Unsafe.html#unsafePerformIO"><span class="hs-identifier hs-var">unsafePerformIO</span></a></span><span> </span><span class="annot"><span class="annottext">(IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
 -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ()))
-&gt; IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-84"></span><span>    </span><span class="hs-keyword">let</span><span> </span><span id="local-6989586621679554240"><span class="annot"><span class="annottext">z :: Int
</span><a href="#local-6989586621679554240"><span class="hs-identifier hs-var hs-var">z</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554244"><span class="hs-identifier hs-var">i</span></a></span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#-"><span class="hs-glyph hs-var">-</span></a></span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554247"><span class="hs-identifier hs-var">sz</span></a></span><span>
</span><span id="line-85"></span><span>    </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554240"><span class="hs-identifier hs-var">z</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span>
</span><span id="line-86"></span><span>      </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-87"></span><span>        </span><span id="local-6989586621679554239"><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554239"><span class="hs-identifier hs-var">b</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IO (MVar ())
forall a. IO (MVar a)
</span><a href="GHC.MVar.html#newEmptyMVar"><span class="hs-identifier hs-var">newEmptyMVar</span></a></span><span>
</span><span id="line-88"></span><span>        </span><span class="annot"><span class="annottext">((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
-&gt; IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554244"><span class="hs-identifier hs-var">i</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554243"><span class="hs-identifier hs-var">b1</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554247"><span class="hs-identifier hs-var">sz</span></a></span><span class="hs-special">,</span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554239"><span class="hs-identifier hs-var">b</span></a></span><span class="hs-special">)</span><span class="annot"><span class="annottext">(Int, MVar ()) -&gt; [(Int, MVar ())] -&gt; [(Int, MVar ())]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554242"><span class="hs-identifier hs-var">b2</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">MVar () -&gt; MaybeMV ()
forall a. MVar a -&gt; MaybeMV a
</span><a href="Control.Concurrent.QSemN.html#JustMV"><span class="hs-identifier hs-var">JustMV</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554239"><span class="hs-identifier hs-var">b</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-89"></span><span>      </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
-&gt; IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), MaybeMV ())
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554240"><span class="hs-identifier hs-var">z</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554243"><span class="hs-identifier hs-var">b1</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554242"><span class="hs-identifier hs-var">b2</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">MaybeMV ()
forall a. MaybeMV a
</span><a href="Control.Concurrent.QSemN.html#NothingMV"><span class="hs-identifier hs-var">NothingMV</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-90"></span><span>
</span><span id="line-91"></span><span>  </span><span class="hs-comment">-- Note: this case match actually allocates the MVar if necessary.</span><span>
</span><span id="line-92"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="annot"><span class="annottext">MaybeMV ()
</span><a href="#local-6989586621679554245"><span class="hs-identifier hs-var">mmvar</span></a></span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-93"></span><span>    </span><span class="annot"><span class="annottext">MaybeMV ()
</span><a href="Control.Concurrent.QSemN.html#NothingMV"><span class="hs-identifier hs-var">NothingMV</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-94"></span><span>    </span><span class="annot"><a href="Control.Concurrent.QSemN.html#JustMV"><span class="hs-identifier hs-type">JustMV</span></a></span><span> </span><span id="local-6989586621679554238"><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554238"><span class="hs-identifier hs-var">b</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">MVar () -&gt; IO ()
</span><a href="#local-6989586621679554237"><span class="hs-identifier hs-var">wait</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554238"><span class="hs-identifier hs-var">b</span></a></span><span>
</span><span id="line-95"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-96"></span><span>    </span><span class="annot"><a href="#local-6989586621679554237"><span class="hs-identifier hs-type">wait</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="GHC.MVar.html#MVar"><span class="hs-identifier hs-type">MVar</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-97"></span><span>    </span><span id="local-6989586621679554237"><span class="annot"><span class="annottext">wait :: MVar () -&gt; IO ()
</span><a href="#local-6989586621679554237"><span class="hs-identifier hs-var hs-var">wait</span></a></span></span><span> </span><span id="local-6989586621679554236"><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554236"><span class="hs-identifier hs-var">b</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-98"></span><span>      </span><span class="annot"><span class="annottext">MVar () -&gt; IO ()
forall a. MVar a -&gt; IO a
</span><a href="GHC.MVar.html#takeMVar"><span class="hs-identifier hs-var">takeMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554236"><span class="hs-identifier hs-var">b</span></a></span><span> </span><span class="annot"><span class="annottext">IO () -&gt; IO () -&gt; IO ()
forall a b. IO a -&gt; IO b -&gt; IO a
</span><a href="Control.Exception.Base.html#onException"><span class="hs-operator hs-var">`onException`</span></a></span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-99"></span><span>        </span><span id="local-6989586621679554234"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554234"><span class="hs-identifier hs-var">already_filled</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">Bool -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#not"><span class="hs-identifier hs-var">not</span></a></span><span> </span><span class="annot"><span class="annottext">(Bool -&gt; Bool) -&gt; IO Bool -&gt; IO Bool
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="Data.Functor.html#%3C%24%3E"><span class="hs-operator hs-var">&lt;$&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">MVar () -&gt; () -&gt; IO Bool
forall a. MVar a -&gt; a -&gt; IO Bool
</span><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier hs-var">tryPutMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554236"><span class="hs-identifier hs-var">b</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-100"></span><span>        </span><span class="annot"><span class="annottext">Bool -&gt; IO () -&gt; IO ()
forall (f :: * -&gt; *). Applicative f =&gt; Bool -&gt; f () -&gt; f ()
</span><a href="GHC.Base.html#when"><span class="hs-identifier hs-var">when</span></a></span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554234"><span class="hs-identifier hs-var">already_filled</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; IO () -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">QSemN -&gt; Int -&gt; IO ()
</span><a href="Control.Concurrent.QSemN.html#signalQSemN"><span class="hs-identifier hs-var">signalQSemN</span></a></span><span> </span><span class="annot"><span class="annottext">QSemN
</span><a href="#local-6989586621679554249"><span class="hs-identifier hs-var">qs</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554247"><span class="hs-identifier hs-var">sz</span></a></span><span>
</span><span id="line-101"></span><span>
</span><span id="line-102"></span><span class="hs-comment">-- |Signal that a given quantity is now available from the 'QSemN'.</span><span>
</span><span id="line-103"></span><span class="annot"><a href="Control.Concurrent.QSemN.html#signalQSemN"><span class="hs-identifier hs-type">signalQSemN</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-type">QSemN</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-104"></span><span class="hs-comment">-- We don't need to mask here because we should *already* be masked</span><span>
</span><span id="line-105"></span><span class="hs-comment">-- here (e.g., by bracket). Indeed, if we're not already masked,</span><span>
</span><span id="line-106"></span><span class="hs-comment">-- it's too late to do so.</span><span>
</span><span id="line-107"></span><span class="hs-comment">--</span><span>
</span><span id="line-108"></span><span class="hs-comment">-- What if the unsafePerformIO thunk is forced in another thread,</span><span>
</span><span id="line-109"></span><span class="hs-comment">-- and receives an asynchronous exception? That shouldn't be a</span><span>
</span><span id="line-110"></span><span class="hs-comment">-- problem: when we force it ourselves, presumably masked, we</span><span>
</span><span id="line-111"></span><span class="hs-comment">-- will resume its execution.</span><span>
</span><span id="line-112"></span><span id="signalQSemN"><span class="annot"><span class="annottext">signalQSemN :: QSemN -&gt; Int -&gt; IO ()
</span><a href="Control.Concurrent.QSemN.html#signalQSemN"><span class="hs-identifier hs-var hs-var">signalQSemN</span></a></span></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="Control.Concurrent.QSemN.html#QSemN"><span class="hs-identifier hs-type">QSemN</span></a></span><span> </span><span id="local-6989586621679554231"><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554231"><span class="hs-identifier hs-var">m</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679554230"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554230"><span class="hs-identifier hs-var">sz0</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-113"></span><span>    </span><span class="hs-comment">-- unsafePerformIO and not unsafeDupablePerformIO. We must not</span><span>
</span><span id="line-114"></span><span>    </span><span class="hs-comment">-- wake up more threads than we're supposed to.</span><span>
</span><span id="line-115"></span><span>  </span><span id="local-6989586621679554229"><span class="annot"><span class="annottext">()
</span><a href="#local-6989586621679554229"><span class="hs-keyword hs-var">unit</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())])
    -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), ()))
-&gt; IO ()
forall a b. IORef a -&gt; (a -&gt; (a, b)) -&gt; IO b
</span><a href="Data.IORef.html#atomicModifyIORef"><span class="hs-identifier hs-var">atomicModifyIORef</span></a></span><span> </span><span class="annot"><span class="annottext">IORef (Int, [(Int, MVar ())], [(Int, MVar ())])
</span><a href="#local-6989586621679554231"><span class="hs-identifier hs-var">m</span></a></span><span> </span><span class="annot"><span class="annottext">(((Int, [(Int, MVar ())], [(Int, MVar ())])
  -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), ()))
 -&gt; IO ())
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())])
    -&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), ()))
-&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span class="hs-special">(</span><span id="local-6989586621679554228"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554228"><span class="hs-identifier hs-var">i</span></a></span></span><span class="hs-special">,</span><span id="local-6989586621679554227"><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554227"><span class="hs-identifier hs-var">a1</span></a></span></span><span class="hs-special">,</span><span id="local-6989586621679554226"><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554226"><span class="hs-identifier hs-var">a2</span></a></span></span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-116"></span><span>            </span><span class="annot"><span class="annottext">IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), ())
-&gt; ((Int, [(Int, MVar ())], [(Int, MVar ())]), ())
forall a. IO a -&gt; a
</span><a href="GHC.IO.Unsafe.html#unsafePerformIO"><span class="hs-identifier hs-var">unsafePerformIO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
-&gt; [(Int, MVar ())]
-&gt; [(Int, MVar ())]
-&gt; IO ((Int, [(Int, MVar ())], [(Int, MVar ())]), ())
forall {a}.
(Num a, Ord a) =&gt;
a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554230"><span class="hs-identifier hs-var">sz0</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Int
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#%2B"><span class="hs-operator hs-var">+</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679554228"><span class="hs-identifier hs-var">i</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554227"><span class="hs-identifier hs-var">a1</span></a></span><span> </span><span class="annot"><span class="annottext">[(Int, MVar ())]
</span><a href="#local-6989586621679554226"><span class="hs-identifier hs-var">a2</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-117"></span><span>
</span><span id="line-118"></span><span>  </span><span class="hs-comment">-- Forcing this will actually wake the necessary threads.</span><span>
</span><span id="line-119"></span><span>  </span><span class="annot"><span class="annottext">() -&gt; IO ()
forall a. a -&gt; IO a
</span><a href="GHC.IO.html#evaluate"><span class="hs-identifier hs-var">evaluate</span></a></span><span> </span><span class="annot"><span class="annottext">()
</span><a href="#local-6989586621679554229"><span class="hs-keyword hs-var">unit</span></a></span><span>
</span><span id="line-120"></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-121"></span><span>   </span><span id="local-6989586621679554225"><span class="annot"><span class="annottext">loop :: a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var hs-var">loop</span></a></span></span><span> </span><span class="annot"><span class="annottext">a
</span><span class="hs-number">0</span></span><span>  </span><span id="local-6989586621679554208"><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554208"><span class="hs-identifier hs-var">bs</span></a></span></span><span> </span><span id="local-6989586621679554207"><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554207"><span class="hs-identifier hs-var">b2</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">((a, [(a, MVar ())], [(a, MVar ())]), ())
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><span class="hs-number">0</span></span><span class="hs-special">,</span><span>  </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554208"><span class="hs-identifier hs-var">bs</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554207"><span class="hs-identifier hs-var">b2</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-122"></span><span>   </span><span class="annot"><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span id="local-6989586621679554206"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554206"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">((a, [(a, MVar ())], [(a, MVar ())]), ())
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554206"><span class="hs-identifier hs-var">sz</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">,</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-123"></span><span>   </span><span class="annot"><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span id="local-6989586621679554205"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554205"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span> </span><span id="local-6989586621679554204"><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554204"><span class="hs-identifier hs-var">b2</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554205"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">[(a, MVar ())] -&gt; [(a, MVar ())]
forall a. [a] -&gt; [a]
</span><a href="GHC.List.html#reverse"><span class="hs-identifier hs-var">reverse</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554204"><span class="hs-identifier hs-var">b2</span></a></span><span class="hs-special">)</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-124"></span><span>   </span><span class="annot"><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span id="local-6989586621679554202"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span id="local-6989586621679554201"><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554201"><span class="hs-identifier hs-var">j</span></a></span></span><span class="hs-special">,</span><span id="local-6989586621679554200"><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554200"><span class="hs-identifier hs-var">b</span></a></span></span><span class="hs-special">)</span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-type">:</span></a></span><span id="local-6989586621679554199"><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554199"><span class="hs-identifier hs-var">bs</span></a></span></span><span class="hs-special">)</span><span> </span><span id="local-6989586621679554198"><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554198"><span class="hs-identifier hs-var">b2</span></a></span></span><span>
</span><span id="line-125"></span><span>     </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554201"><span class="hs-identifier hs-var">j</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; a -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3E"><span class="hs-operator hs-var">&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-126"></span><span>       </span><span id="local-6989586621679554196"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554196"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar () -&gt; IO Bool
forall a. MVar a -&gt; IO Bool
</span><a href="GHC.MVar.html#isEmptyMVar"><span class="hs-identifier hs-var">isEmptyMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554200"><span class="hs-identifier hs-var">b</span></a></span><span>
</span><span id="line-127"></span><span>       </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554196"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">((a, [(a, MVar ())], [(a, MVar ())]), ())
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554201"><span class="hs-identifier hs-var">j</span></a></span><span class="hs-special">,</span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554200"><span class="hs-identifier hs-var">b</span></a></span><span class="hs-special">)</span><span class="annot"><span class="annottext">(a, MVar ()) -&gt; [(a, MVar ())] -&gt; [(a, MVar ())]
forall a. a -&gt; [a] -&gt; [a]
</span><a href="../../ghc-prim/src/GHC.Types.html#%3A"><span class="hs-glyph hs-var">:</span></a></span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554199"><span class="hs-identifier hs-var">bs</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554198"><span class="hs-identifier hs-var">b2</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-128"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554199"><span class="hs-identifier hs-var">bs</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554198"><span class="hs-identifier hs-var">b2</span></a></span><span>
</span><span id="line-129"></span><span>     </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-130"></span><span>       </span><span id="local-6989586621679554195"><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554195"><span class="hs-identifier hs-var">r</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">MVar () -&gt; () -&gt; IO Bool
forall a. MVar a -&gt; a -&gt; IO Bool
</span><a href="GHC.MVar.html#tryPutMVar"><span class="hs-identifier hs-var">tryPutMVar</span></a></span><span> </span><span class="annot"><span class="annottext">MVar ()
</span><a href="#local-6989586621679554200"><span class="hs-identifier hs-var">b</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-131"></span><span>       </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="#local-6989586621679554195"><span class="hs-identifier hs-var">r</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span><span class="annot"><span class="annottext">a -&gt; a -&gt; a
forall a. Num a =&gt; a -&gt; a -&gt; a
</span><a href="GHC.Num.html#-"><span class="hs-glyph hs-var">-</span></a></span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554201"><span class="hs-identifier hs-var">j</span></a></span><span class="hs-special">)</span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554199"><span class="hs-identifier hs-var">bs</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554198"><span class="hs-identifier hs-var">b2</span></a></span><span>
</span><span id="line-132"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">a
-&gt; [(a, MVar ())]
-&gt; [(a, MVar ())]
-&gt; IO ((a, [(a, MVar ())], [(a, MVar ())]), ())
</span><a href="#local-6989586621679554225"><span class="hs-identifier hs-var">loop</span></a></span><span> </span><span class="annot"><span class="annottext">a
</span><a href="#local-6989586621679554202"><span class="hs-identifier hs-var">sz</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554199"><span class="hs-identifier hs-var">bs</span></a></span><span> </span><span class="annot"><span class="annottext">[(a, MVar ())]
</span><a href="#local-6989586621679554198"><span class="hs-identifier hs-var">b2</span></a></span><span>
</span><span id="line-133"></span></pre></body></html>